<?php
require_once("mysql.php");
require_once("mysql_table.class.php");

abstract class mysql_table{

    protected $fields;
    protected $TableName;
    protected $IDField;
    protected $SQLObject;
    public $viewrows; //para el paginamiento!

    function __construct(){
        $this->SQLObject = new mySQL();
        
        
    }

    function lastquery(){
        return $this->SQLObject->lastquery;
    }

    function getdata($result){
        $reg = $this->SQLObject->fetch_assoc($result);
        foreach($this->fields as $fi=>$V) $this->fields[$fi] = $reg[$fi];
    }
    
    function getlastid($query=""){
        if ($this->SQLObject->connect()){
            $query="SELECT ".$this->IDField." +1 AS NEWID FROM ".$this->TableName." ORDER BY ".$this->IDField." DESC LIMIT 1";

            $result = $this->SQLObject->query($query);
            $fila = $this->SQLObject->fetch_assoc($result);

            if ($this->SQLObject->num_rows($result)>0){
                $this->SQLObject->close();
                return $fila['NEWID'];
            }else{
                $this->SQLObject->close();
                return 0;
            }
        }
    }        
 

    function search($ID){
        $query = "SELECT * FROM " . $this->TableName ." WHERE ".$this->IDField."=" . $ID;
        $result = $this->runquery($query);
       //print_r($query);
        if ($this->SQLObject->num_rows($result) == 0) {
            return false;
        } else {
            $this->getdata($result);
            return true;
        }
    }
    
    function search_by($Column,$Value){
        $query = "SELECT * FROM " . $this->TableName ." WHERE ".$Column."=" . $Value;
        $result = $this->runquery($query);
        //print_r($query);
        if ($this->SQLObject->num_rows($result) == 0) {
            return false;
        } else {
            $this->getdata($result);
            return true;
        }
    }    
	
	

    Function runquery($query){
    //echo $query;
    //echo "<br>";
        if ($this->SQLObject->connect()){
            $resource = $this->SQLObject->query($query);
            if ($resource){
                $this->SQLObject->close();
                return $resource;
            }else{
                $this->SQLObject->close();
                return false;
            }           
        }else{
            return false;
        }
    }

    private function v_add()
    {
        foreach($this->fields as $fi) $Buffer[] = $fi;
        return $Buffer;
    }

    private function v_modify(){
                
        foreach($this->fields as $Key=>$a) {
            if (is_string($a)){
                $buffer[] = "$Key='".$this->SQLObject->escape_string($a)."'";
            }else{
                $buffer[] = "$Key=$a";
            }
            
        }
        return $buffer;
    }

    function add(){
        $query = "INSERT INTO ".$this->TableName." VALUES ('".implode('\',\'',$this->v_add())."')";
    //echo $query;
    //exit();
        if ($this->runquery($query)){
            return 1;
        }else{
            return 0;
        }            
    }

    function modify()
    {
        
        $query = "UPDATE " . $this->TableName . " SET ". implode(' , ',$this->v_modify()) ." WHERE $this->IDField = ".$this->fields[$this->IDField];
        //print_r($query);
        
        
        if ($this->runquery($query)) {
            return 1;
        } else {
            return 0;
        }

    }

    function delete()
    {        
        $query = "DELETE FROM $this->TableName WHERE $this->IDField=".$this->fields[$this->IDField];
        
        if ($this->runquery($query)) {
            return 1;
        } else {
        $this->SQLObject->Error;
            return 0;
        }
        
    }
    
    function delete_all()
    {        
        $query = "DELETE FROM ".$this->TableName;
        
        if ($this->runquery($query)) {
            return 1;
        } else {
        $this->SQLObject->Error;
            return 0;
        }
        
    }

    function tovector_assoc($fields="", $condition="", $groupby="",$orderby="", $limit="",$paginado = 0)
    {
        $query_count = "SELECT count($this->IDField) FROM $this->TableName ";        
        
        
        if ($fields == "") {
            $query = "SELECT * FROM $this->TableName ";
        } else {
            if (is_array($fields)){
                $query = "SELECT " . implode(",", $fields) . " FROM $this->TableName ";
            }else{
                $query = "SELECT " . $fields . " FROM $this->TableName ";
            }
        }
        
        
        if ($condition == "") {
        } else {
            if (is_array($condition)){
                $query_count .= "WHERE " . implode(" AND ", $condition);
                $query .= "WHERE " . implode(" AND ", $condition);
             
                
            }else{
                $query_count .= "WHERE " . $condition;
                $query .= "WHERE " . $condition;
                
            }
          
        }

        if ($groupby =="") {
        }else{
            if(is_array($groupby)){
                $query_count .= " GROUP BY " . implode(" , ", $groupby);    
                $query .= " GROUP BY " . implode(" , ", $groupby);
            }else{
                $query_count .= " GROUP BY " . $groupby;
                $query .= " GROUP BY " . $groupby;
            }
        }

        if ($orderby =="") {
        }else{
            if(is_array($orderby)){
                $query .= " ORDER BY " . implode(" , ", $orderby);
            }else{
                $query .= " ORDER BY " . $orderby;
            }
        }
        
        
             
        
        if ($limit =="") {
        }else{
            $query .= " LIMIT " . $limit;
        }
     
     
      

        $resource = $this->runquery($query);
     
     
        if($paginado){
            $resource_count = $this->runquery($query_count);
            if($resource_count){
                $cnt = mysql_fetch_row($resource_count);
                $this->viewrows = $cnt[0];    
            }else{
                $this->viewrows = 0;
            }
                 
        }else{
            $this->viewrows = mysql_num_rows($resource);    
        }   
        

        if ($resource){
            if($this->SQLObject->num_rows($resource)){
                while($res = $this->SQLObject->fetch_assoc($resource)){
                    $fields = array_keys($res);
                    $i = 0;
                    foreach ($fields as $field){
                        $a[$field][]= $res[$field];
                        $i ++;
                    }
                }
                return $a;
            }else{
                return 0;
            }
        }else{
            return 0;
        }
        
     echo $query;   
        
    }

    function tovector_row($fields="", $condition="", $groupby="",$orderby="",$limit="")
    {

        if ($fields == "") {
            $query = "SELECT * FROM $this->TableName ";
        } else {
            if (is_array($fields)){
                $query = "SELECT " . implode(",", $fields) . " FROM $this->TableName ";
            }else{
                $query = "SELECT " . $fields . " FROM $this->TableName ";
            }
        }
        if ($condition == "") {
        } else {
            if (is_array($condition)){
                $query .= "WHERE " . implode(" AND ", $condition);
            }else{
                $query .= "WHERE " . $condition;
            }
        }

        if ($groupby =="") {
        }else{
            if(is_array($groupby)){
                $query .= " GROUP BY " . implode(" , ", $groupby);
            }else{
                $query .= " GROUP BY " . $groupby;
            }
        }

        if ($orderby =="") {
        }else{
            if(is_array($orderby)){
                $query .= " ORDER BY " . implode(" , ", $orderby);
            }else{
                $query .= " ORDER BY " . $orderby;
            }
        }

        if ($limit =="") {
        }else{
            $query .= " LIMIT " . $limit;
        }
        

        $resource = $this->runquery($query);
        
        if ($resource){
            if($this->SQLObject->num_rows($resource)){
                while($res = $this->SQLObject->fetch_row($resource)){
                        $fields = array_keys($res);
                        $i = 0;
                        foreach ($fields as $field){
                            $a[$i][]= $res[$i];
                            $i ++;
                        }
                 }
                 return $a;
             }else{
                 return 0;
             }
        }else{
            return 0;
        }
    }


    protected function tovector_assoc_query($query){
        $resource = $this->runquery($query);
        if ($resource){
            if(mysql_num_rows($resource)){
                while($res = mysql_fetch_assoc($resource)){
                    $fields = array_keys($res);
                    $i = 0;
                    foreach ($fields as $field){
                        $a[$field][]= $res[$field];
                        $i ++;
                    }
                }
                return $a;
            }else{
                return 0;
            }
        }else{
            return 0;
        }        
        
    } 

    public function __get($Name){        
            return @$this->fields[$Name];
    }

    public function __set($Name,$Value){
            $this->fields[$Name] = $Value;
    }

    function list_all(){
        
        $query = "SELECT * FROM " . $this->TableName;
        $salida = $this->tovector_assoc_query($query);
        return $salida;      
    }
}


?>
